package mhr;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class HelloWorld {

	private File propertyFile;
	private Properties properties;
	private JTextField textField;
	private JFrame frame;
	private JLabel statusLabel;

	public HelloWorld() {
		File lockFile = new File(new File(System.getProperty("user.home")),
				this.getClass().getName() + ".lock");
			
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(lockFile, "rws");
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		FileChannel channel = raf.getChannel();
		FileLock lock = null;
		try {
			lock = channel.tryLock(0, 1, false);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		if (lock == null) {
			System.out.println("Only one instance allowed..");
			System.exit(0);
		}
		
		
		System.out.println("Hello World!!");

		properties = new Properties();

		propertyFile = new File("suncertify.properties");

		try {
			propertyFile.createNewFile();
			properties.load(new FileInputStream(propertyFile));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		final JPanel mainPanel = new JPanel(new BorderLayout());
		
		final JPanel rowPanel = new JPanel();
		rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.Y_AXIS));
		rowPanel.setBorder(BorderFactory.createTitledBorder("Database Setup:"));
		
		JPanel dbPanel = new JPanel();
		dbPanel.add(new JLabel("Location: "));
		rowPanel.add(dbPanel);
		
		Action okAction = new AbstractAction("Ok") {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("ok pressed..");
				
				if (validateFile()) {
					storePropertyFile();
					frame.setVisible(false);
					
					//Start db
					System.out.println("Starting Database: " + textField.getText());
				}
				else {
					statusLabel.setText("File does not exit..");
				}
				
				
			}
		};
		
		Action cancelAction = new AbstractAction("Cancel") {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("cancel pressed..");
				System.exit(0);
			}
		};
		
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(new JButton(okAction));
		buttonPanel.add(new JButton(cancelAction));
		
		
		
		rowPanel.add(buttonPanel);
		
		JPanel statusPanel = new JPanel();
		statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));

		statusLabel = new JLabel("Select database.");

		statusPanel.add(Box.createHorizontalStrut(4));
		statusPanel.add(statusLabel);
		statusPanel.add(Box.createVerticalStrut(21));

		statusPanel.setBorder(BorderFactory.createLoweredBevelBorder());

		mainPanel.add(statusPanel, BorderLayout.SOUTH);
		
		//Create a file chooser
		final JFileChooser fc = new JFileChooser();
		

		textField = new JTextField(properties.getProperty("dbfile"));
		textField.setColumns(25);
		
		
		dbPanel.add(textField);
		mainPanel.add(rowPanel, BorderLayout.CENTER);
		JButton openButton = new JButton("Open File...");
		openButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				fc.showOpenDialog(mainPanel);
				try {
					textField.setText(fc.getSelectedFile().getCanonicalPath().toString());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		});
		dbPanel.add(openButton);
		
		
		
		frame = new JFrame("Bodgitt & Scarper - Setup Database");
		frame.add(mainPanel);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true); 	
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				
				System.out.println("windowClosing: " + e);
				
				
				
			}
		
		
		});
		
		
	}

	public void show() {
		
	}
	
	private boolean validateFile() {
		//for now, just check if file exist
		File file = new File(textField.getText());
		return file.exists();
		
	}
	
	private void storePropertyFile() {
		properties.setProperty("dbfile", textField.getText());
		
		try {
			properties.store(new FileOutputStream(propertyFile), "Store DB path");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		System.out.println("dbpath: " + properties.getProperty("dbfile"));

	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new HelloWorld();	
	}

}
